The invention relates generally to computer systems, and more particularly to memory management in computer systems.
Application programs often allocate memory that is not used, or is used briefly and then unused thereafter. As memory is a resource that may become scarce, application programs are supposed to deallocate memory that is no longer needed. However, applications often fail to do so, and this memory misuse leads to low memory conditions and otherwise degrades system performance. Applications also tend to access memory after they manually free it, which also causes major problems.
The concept of xe2x80x9cgarbage collectionxe2x80x9d has been developed to automatically manage application memory by reclaiming memory space allocated to applications that is not being used. Garbage collection operates on behalf of the application, without the application""s assistance, to look for objects that are unused. A garbage collector operates, by scanning for cross-generation pointers in memory, which indicate an object still in use. One type of garbage collection is sequential in nature, wherein a garbage collection mechanism runs whenever memory is needed. While the collection mechanism is run to analyze the memory (e.g., a set of allocated objects) that is not being used, the application is temporarily halted so that it cannot be modifying memory. A significant problem with sequential garbage collection is that the application often experiences inconvenient and/or undesirable pauses during the collection operation.
Another type of garbage collection is concurrent in nature, wherein the garbage collectors run at the same time as the application and collect only a portion of unused memory at a time. Only when the collector has done the bulk of its work is the application temporarily halted to prevent it from writing to memory just as that memory is being freed, whereby the application is not significantly paused. To look for objects that are unused, the garbage collector enumerates locations that have been written into so it can scan for the cross-generation pointers, i.e., rather than scan large amounts of system memory, only changed memory is examined. However, this requires a more complex collector to concurrently track memory that is being actively used by an application, and also requires multiple passes to locate any memory earlier determined to be unused but that an application has since used while the collector was performing other work.
To track which memory has changed with contemporary operating systems and microprocessors, write-protect and write-watch are techniques that have been attempted. Write-protect generally operates by protecting sections of memory (e.g., pages) allocated to an application. Then, whenever the application writes to a protected page, a page fault is triggered. By the page fault, the collector thus knows that this page was written to, and can record the page as changed, e.g., in some data structure used for tracking changed pages. The collector then unprotects the page to allow the change and allow the application to use it. Some time later, the collector will free unused memory and reset the tracking process. Conventional write-watch is somewhat similar to write-protect, except that write-watch tracks memory usage without protecting the page and generating the page fault exception.
While write-protect and write-watch thus enable concurrent garbage collection mechanisms, such mechanisms have heretofore been highly inefficient. Indeed, write-protect is significantly slower than write-watch. At the same time, past write-watch techniques have degraded system performance so significantly that that a number of write-watch garbage collection efforts have been abandoned.
Briefly, the present invention provides a method and system that enables an efficient write-watch mechanism, while adding as little as one bit per virtual page address being watched, and that operates without substantially degrading performance even on large address ranges. To this end, a bitmap is associated with the Virtual Address Descriptor (VAD) for a process, one bit for each virtual page address allocated to a process with write-watch enabled. As part of the write-watch mechanism if a virtual address is trimmed to disk and that virtual address page is marked as modified, then the corresponding bit in the VAD is set for that virtual address page. Only when a modified page is trimmed is the bitmap accessed during normal system operation, providing extremely fast write watching.
The memory manager may receive an API call (e g., from a garbage collection mechanism) seeking to know which virtual addresses in a process have been modified since last checked, e.g., since the last time the garbage collection mechanism asked. To determine this, the memory manager walks the bitmap in the relevant VAD for the specified virtual address range for the requested process. If a bit is set, then the page corresponding to that bit is known to have been modified since last asked. The bit is cleared in the VAD bitmap (if specified by the API), and a result returned for that page, (e.g., the page number is added to an array that is returned).
If the bit is not set, then there is still a chance that the page was modified, just not trimmed. To determine if the page was modified, the page table entry (PTE) is checked for that page, and if the PTE indicates the page was modified, a corresponding bit is set in the PFN database, the modified bit in the PTE is cleared, (if reset is requested by the API, any other processors are interrupted and the result may be returned for that virtual page address. Otherwise that page is known to be unmodified since the last call.
One enhancement to the present invention looks at the page directory tables corresponding to the write-watched pages for that process for which status has been requested, each of which indicates whether a group of (e.g., 1024) pages have been trimmed. If the pages have been trimmed, then any zero bits in the VAD corresponding to this group are known to be unmodified, since any moidfied, trimmed page would have had its bit set in the VAD when trimmed. The portion of the VAD bitmap corresponding to the trimmed page directory thus reflects the modified state of pages in this page directory, whereby the PTE need not be checked for that portion. An appropriate result is returned to the caller, and the bitmap portion cleared (if requested) so that it will reflect whether it has been modified since the time last asked.